home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Draw Editor / Source / Command.cpp next >
Encoding:
Text File  |  1995-12-11  |  7.8 KB  |  351 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Palette.cpp
  3.  
  4.     Contains:    Palette Classes Implementation
  5.  
  6.     Written by:    Dave Stafford
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. // -- Compiler/Preprocessor Switches --
  12.  
  13. #ifndef _COMPILERDEFS_
  14. #include "CompDefs.h"
  15. #endif
  16.  
  17. // -- DrawEditor Includes --
  18.  
  19. #ifndef _COMMAND_
  20. #include "Command.h"
  21. #endif
  22.  
  23. #ifndef _DRAWEDITORGLOBALS_
  24. #include "DrawEditorGlobals.h"
  25. #endif
  26.  
  27. #ifndef _DRAWEDITOR_
  28. #include "DrawEditor.h"
  29. #endif
  30.  
  31. #ifndef _DRAWEDITORUTILS_
  32. #include "DrawEditorUtils.h"
  33. #endif
  34.  
  35. // -- OpenDoc Includes --
  36.  
  37. // for ODName
  38. #ifndef _ODTYPES_
  39. #include <ODTypes.h>
  40. #endif
  41.  
  42. #ifndef _ITEXT_
  43. #include "IText.h"
  44. #endif
  45.  
  46. #ifndef SOM_ODUndo_xh
  47. #include <Undo.xh>
  48. #endif
  49.  
  50. #ifndef SOM_ODFrame_xh
  51. #include <Frame.xh>
  52. #endif
  53.  
  54. #ifndef SOM_ODSession_xh
  55. #include <ODSessn.xh>
  56. #endif
  57.  
  58. #ifndef SOM_ODInfo_xh
  59. #include <Info.xh>
  60. #endif
  61.  
  62. #ifndef SOM_ODPart_xh
  63. #include <Part.xh>
  64. #endif
  65.  
  66. // -- OpenDoc Utilities --
  67.  
  68. #ifndef _ODDEBUG_
  69. #include "ODDebug.h"
  70. #endif
  71.  
  72. #ifndef _ODMEMORY_
  73. #include "ODMemory.h"            // ODDisposePtr
  74. #endif
  75.  
  76. #ifndef _BARRAY_
  77. #include <BArray.h>
  78. #endif
  79.  
  80. #ifndef _USERSRCM_
  81. #include "UseRsrcM.h"
  82. #endif
  83.  
  84. #ifndef _ITEXT_
  85. #include "IText.h"
  86. #endif
  87.  
  88. // -- Toolbox Includes --
  89.  
  90. #ifndef mathRoutinesIncludes
  91. #include <math routines.h>
  92. #endif
  93.  
  94. #ifndef __MEMORY__
  95. #include <memory.h>
  96. #endif
  97.  
  98.  
  99. //-----------------------------------------------------------------------------
  100. // CCommand::CCommand
  101. //-----------------------------------------------------------------------------
  102.  
  103. CCommand::CCommand(DrawEditor* theEditor, 
  104.                 ODBoolean canUndo,
  105.                 ODBoolean changesContent,
  106.                 ODID undoTextIndex,
  107.                 ODID redoTextIndex)
  108. {
  109.     fDrawEditor = theEditor;
  110.     fCanUndo = canUndo;
  111.     
  112.     fChangesContent = changesContent;
  113.     fActionType = kODSingleAction;
  114.     
  115.     fUndoTextIndex = undoTextIndex;
  116.     fRedoTextIndex = redoTextIndex;
  117.     
  118.     fDoneState = 0;
  119.     
  120.     fUndo = kODNULL;
  121. }
  122.  
  123.  
  124. //-----------------------------------------------------------------------------
  125. // CCommand::~CCommand
  126. //-----------------------------------------------------------------------------
  127.  
  128. CCommand::~CCommand()
  129. {
  130.     fDrawEditor = NULL;
  131. }
  132.  
  133.  
  134. //-----------------------------------------------------------------------------
  135. // CCommand::SetActionType
  136. //-----------------------------------------------------------------------------
  137.  
  138. void CCommand::SetActionType(ODActionType actionType)
  139. {
  140.     fActionType = actionType;
  141. }
  142.  
  143.  
  144. //-----------------------------------------------------------------------------
  145. // CCommand::SetChangesContent
  146. //-----------------------------------------------------------------------------
  147.  
  148. void CCommand::SetChangesContent(ODBoolean changesContent)
  149. {
  150.     fChangesContent = changesContent;
  151. }
  152.  
  153.  
  154. //-----------------------------------------------------------------------------
  155. // CCommand::SetMenuTextIndexs
  156. //-----------------------------------------------------------------------------
  157.  
  158. void CCommand::SetMenuTextIDs(ODID undoTextIndex, ODID redoTextIndex)
  159. {
  160.     fUndoTextIndex = undoTextIndex;
  161.     fRedoTextIndex = redoTextIndex;
  162. }
  163.  
  164.  
  165. //-----------------------------------------------------------------------------
  166. // CCommand::GetActionType
  167. //-----------------------------------------------------------------------------
  168.  
  169. ODActionType CCommand::GetActionType() const
  170. {
  171.     return fActionType;
  172. }
  173.  
  174.  
  175. //-----------------------------------------------------------------------------
  176. // CCommand::CanUndo
  177. //-----------------------------------------------------------------------------
  178.  
  179. ODBoolean CCommand::CanUndo() const
  180. {
  181.     return fCanUndo;
  182. }
  183.  
  184.  
  185. //-----------------------------------------------------------------------------
  186. // CCommand::ChangesContent
  187. //-----------------------------------------------------------------------------
  188.  
  189. ODBoolean CCommand::ChangesContent() const
  190. {
  191.     return fChangesContent;
  192. }
  193.  
  194.  
  195. //-----------------------------------------------------------------------------
  196. // CCommand::AbortCommand
  197. //-----------------------------------------------------------------------------
  198.  
  199. void CCommand::AbortCommand(Environment* ev)
  200. {
  201.     // Abort this transaction
  202.     fUndo->AbortCurrentTransaction(ev);
  203. }
  204.  
  205.  
  206. //-----------------------------------------------------------------------------
  207. // CCommand::WriteAction
  208. //-----------------------------------------------------------------------------
  209.  
  210. void CCommand::WriteAction(Environment* ev, ODActionType actionType)
  211. {
  212.     Str255    text;
  213.     ODName*    undoString = kODNULL;
  214.     ODName*    redoString = kODNULL;
  215.     
  216.     ODSLong tRef;
  217.     tRef = BeginUsingLibraryResources();
  218.     
  219.     TRY
  220.     
  221.         // Get the undo string
  222.         ::GetIndString(text, kMenuStringResID, fUndoTextIndex);
  223.         undoString = CreateIText(gGlobals->fEditorsScript, gGlobals->fEditorsLanguage, (StringPtr)&text);
  224.         
  225.         // Get the redo string
  226.         GetIndString(text, kMenuStringResID, fRedoTextIndex);
  227.         redoString = CreateIText(gGlobals->fEditorsScript, gGlobals->fEditorsLanguage, (StringPtr)&text);
  228.         
  229.         // Make sure we don't pass NULL menu strings to OpenDoc!
  230.         THROW_IF_NULL(undoString);
  231.         THROW_IF_NULL(redoString);
  232.  
  233.         // Assume that the same command is used for begin & end actions
  234.         void* temp = kODNULL;
  235.         if (actionType!=kODEndAction)
  236.             temp = &this;
  237.         else
  238.             temp = &(gGlobals->fZeroLong);
  239.         
  240.         ODActionData actionState;
  241.         
  242.         // Set up the action data
  243.         actionState = ::CreateByteArrayStruct(temp, sizeof(temp));
  244.                 
  245.         fUndo->AddActionToHistory(ev, 
  246.                                   fDrawEditor->GetODPart(),        /* whichPart */
  247.                                   &actionState,                    /* actionData */
  248.                                   actionType,                    /* actionType */
  249.                                   undoString,                    /* undoActionLabel */
  250.                                   redoString);                    /* redoActionLabel */
  251.     
  252.         DisposeByteArrayStruct(actionState);
  253.     
  254.     CATCH_ALL
  255.         ODSetSOMException(ev, ErrorCode());
  256.     
  257.         DebugStr("\pSetMenuItemText Failed.");
  258.     ENDTRY;
  259.     
  260.     EndUsingLibraryResources(tRef);
  261.  
  262.     // Delete the IText data
  263.     DisposeIText(undoString);
  264.     DisposeIText(redoString);
  265. }
  266.  
  267.  
  268. //-----------------------------------------------------------------------------
  269. // CCommand::DoCommand
  270. //-----------------------------------------------------------------------------
  271.  
  272. void CCommand::DoCommand(Environment* ev)
  273. {
  274.  
  275.     // Capture the command state
  276.     this->CaptureCommandState(ev);
  277.     
  278.     // for convenience, keep a reference to ODUndo
  279.     if (this->CanUndo())
  280.     {
  281.         fUndo = fDrawEditor->GetSession(ev)->GetUndo(ev);
  282.     }
  283.  
  284.     if (fChangesContent)
  285.         fDrawEditor->SetDirty(ev);
  286.         
  287.     if (fCanUndo)
  288.     {
  289.         this->WriteAction(ev, fActionType);
  290.     }
  291.  
  292.     fDoneState = kODDone;
  293. }
  294.  
  295.  
  296. //-----------------------------------------------------------------------------
  297. // CCommand::UndoCommand
  298. //-----------------------------------------------------------------------------
  299.  
  300. void CCommand::UndoCommand(Environment* ev)
  301. {
  302.     fDoneState = kODUndone;
  303. }
  304.  
  305.  
  306. //-----------------------------------------------------------------------------
  307. // CCommand::RedoCommand
  308. //-----------------------------------------------------------------------------
  309.  
  310. void CCommand::RedoCommand(Environment* ev)
  311. {
  312.     fDoneState = kODRedone;
  313. }
  314.  
  315.  
  316. //-----------------------------------------------------------------------------
  317. // CCommand::Commit
  318. //
  319. // Here you want to clean up command specific structures depnding on the value of state.
  320. // For example, if a command creates a new piece of content and is comitted after being undone
  321. // then it should delete the structure representing the new content. Otherwise, it should not
  322. // deleted.
  323. //-----------------------------------------------------------------------------
  324.  
  325. void CCommand::Commit(Environment* ev, ODDoneState state)
  326. {
  327.     if (fChangesContent && (state == kODDone)||(state == kODRedone))
  328.     {
  329.         fDrawEditor->SetDirty(ev);
  330.     }
  331. }
  332.  
  333.  
  334. //-----------------------------------------------------------------------------
  335. // CCommand::CaptureCommandState
  336. //
  337. // Here you want to save off any state info necessary for the command to be able to undo/redo.
  338. // Example: You might make a copy of a list of references to content that is being operated on
  339. // by the command.
  340. //-----------------------------------------------------------------------------
  341.  
  342. void CCommand::CaptureCommandState(Environment* ev)
  343. {
  344.  
  345. }
  346.  
  347.  
  348.  
  349.  
  350.  
  351.